<?php

namespace app\storeapi\controller;
use app\store\model\Wxapp as WxappModel;
use app\storeapi\model\Category as CategoryModel;
use app\storeapi\model\Goods as GoodsModel;
use app\storeapi\service\Goods as GoodsService;
use app\storeapi\validate\Goods as GoodsValidate;


/**
 * 商品管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Goods extends Controller
{
    /**
     * 商品列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function goodsList()
    {
        // 获取全部商品列表
        $model = new GoodsModel;
        $list = $model->getGoodsList($this->postData());
        $catgory = CategoryModel::getCacheTree();
        return $this->renderSuccess( compact('list', 'catgory'));

    }

    /**
     * 删除商品
     * @param $goods_id
     * @param boolean $goodsDel
     * @return array
     */
    public function goodsDel()
    {
        $param = $this->postData();
        $model = new GoodsModel;
        $validate = new GoodsValidate;
        // 表单验证
        if (!$validate->scene('del')->check($param)) {
            return $this->renderError($validate->getError());
        }
        // 商品详情
        $goodsInfo = $model->detail($param);
        if(empty($goodsInfo)){
            return $this->renderError('商品不存在，请刷新后重试');
        }

        if (!$goodsInfo->goodsDel($param)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 修改商品状态
     * @param $goods_id
     * @param boolean $state
     * @return array
     */
    public function status()
    {
        $param = $this->postData();
        $model = new GoodsModel;
        $validate = new GoodsValidate;
        // 表单验证
        if (!$validate->scene('status')->check($param)) {
            return $this->renderError($validate->getError());
        }
        // 商品详情
        $goodsInfo = $model->detail($param);
        if(empty($goodsInfo)){
            return $this->renderError('商品不存在，请刷新后重试');
        }

        if (!$goodsInfo->setStatus($param)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 商品详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function goodsInfo()
    {
        // 商品详情
        $model = new GoodsModel;
        $detail = $model->detail($this->postData());

        //获取该商户的分销方案
        $dealerCase = WxappModel::dealerCase($this->wxapp_id);

        if(empty($detail)){
            return $this->renderError('商品不存在，请刷新后重试');
        }
        $detail['content'] = htmlspecialchars_decode($detail['content']);
        //$data = GoodsService::getEditData($detail);
        $data = array_merge(GoodsService::getEditData($detail), compact('detail','dealerCase'));
        return $this->renderSuccess($data);
    }


    /**
     * 获取所需数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getData()
    {
        $data = array_merge(GoodsService::getEditData(null, 'add'), []);
        return $this->renderSuccess($data);
    }


    /**
     * 商品添加
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function goodsAdd()
    {
        //获取数据
        $params = $this->postData();
        $validate = new GoodsValidate;
        // 表单验证
        if (!$validate->scene('addInfo')->check($params)) {
            return $this->renderError($validate->getError());
        }
        // 商品添加
        $flag = (new GoodsModel)->add($params);
        if($flag){
            return $this->renderSuccess([],'添加成功');
        }else{
            return $this->renderError('添加失败');
        }
    }


    /**
     * 商品编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function goodsEdit()
    {
        //获取数据
        $params = $this->postData();
        $validate = new GoodsValidate;
        // 表单验证
        if (!$validate->scene('editInfo')->check($params)) {
            return $this->renderError($validate->getError());
        }
        // 商品详情
        $flag = (new GoodsModel)->edit($params);
        if($flag){
            return $this->renderSuccess([],'编辑成功');
        }else{
            return $this->renderError('编辑失败');
        }
    }



}



